package com.kuang.utils;

import com.kuang.mapper.AstImportControllerLogMapper;
import com.kuang.pojo.AstImportControllerLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

/**
 * zoe_zls
 * 2024/3/16
 */
@Component
public class ControllerLogUtils {

    @Resource
    AstImportControllerLogMapper astImportControllerLogMapper;

    //静态方法加载service/mapper
    private static ControllerLogUtils controllerLogUtils;

    @PostConstruct
    public void init() {
        controllerLogUtils = this;
    }

    public static int setMsgLog(String controller_type, String controller_code, String controller_name, String msg, String json, String return_json) {
        AstImportControllerLog astImportControllerLog = new AstImportControllerLog();
        astImportControllerLog.setAst_import_controller_log_id(String.valueOf(UUID.randomUUID()));
        astImportControllerLog.setCreate_time(new Date());
        astImportControllerLog.setStatus("true");
        astImportControllerLog.setController_type(controller_type);
        astImportControllerLog.setController_code(controller_code);
        astImportControllerLog.setController_name(controller_name);
        astImportControllerLog.setMsg(msg);
        astImportControllerLog.setReturn_json(return_json);

        if (StringUtils.equals("post", controller_type)) {
            astImportControllerLog.setPost_json(json);
        } else if (StringUtils.equals("import", controller_type)) {
            astImportControllerLog.setImpot_json(json);
        }

        return controllerLogUtils.astImportControllerLogMapper.insert(astImportControllerLog);
    }

    public static int setErrorMsgLog(String controller_type, String controller_code, String controller_name, String error_msg, String json, String return_json) {
        AstImportControllerLog astImportControllerLog = new AstImportControllerLog();
        astImportControllerLog.setAst_import_controller_log_id(String.valueOf(UUID.randomUUID()));
        astImportControllerLog.setCreate_time(new Date());
        astImportControllerLog.setStatus("false");
        astImportControllerLog.setController_type(controller_type);
        astImportControllerLog.setController_code(controller_code);
        astImportControllerLog.setController_name(controller_name);
        astImportControllerLog.setError_msg(error_msg);
        astImportControllerLog.setReturn_json(return_json);

        if (StringUtils.equals("post", controller_type)) {
            astImportControllerLog.setPost_json(json);
        } else if (StringUtils.equals("import", controller_type)) {
            astImportControllerLog.setImpot_json(json);
        }
        return controllerLogUtils.astImportControllerLogMapper.insert(astImportControllerLog);
    }

    public static String reverseString(String s) {
        return new StringBuilder(s).reverse().toString();
    }

}
